package com.boyi.shared.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class JsonUtil {

    public static Logger log = LoggerFactory.getLogger(JsonUtil.class);

    /**
     * @param json key
     * @author 颜培轩
     * @date 2018/07/31
     */
    public static String getJsonValue(String json, String key) {

        JSONObject o = JSON.parseObject(json);
        if (key.indexOf('.') == -1) {
            try {
                json = o.get(key).toString();
            } catch (Exception e) {
                log.info("key: " + key + " is not exist");
                return null;
            }
            return json;
        } else {
            String keyName = key.substring(0, key.indexOf('.'));
            try {
                json = o.get(keyName).toString();
            } catch (Exception e) {
                log.info("key:" + keyName + " is not exist");
                return null;
            }
            key = key.substring(key.indexOf('.') + 1);
            return getJsonValue(json, key);
        }
    }

}
